package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbGoodsImageMapper;
import com.bwie.pojo.TbGoods;
import com.bwie.pojo.TbGoodsImage;
import com.bwie.service.TbGoodsService;
import com.bwie.mapper.TbGoodsMapper;
import com.bwie.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 28242
* @description 针对表【tb_goods】的数据库操作Service实现
* @createDate 2025-05-27 16:56:09
*/
@Service
public class TbGoodsServiceImpl extends ServiceImpl<TbGoodsMapper, TbGoods>
    implements TbGoodsService{

    @Autowired
    TbGoodsImageMapper tbGoodsImageMapper;
    @Autowired
    TbGoodsMapper tbGoodsMapper;


    @Override
    public Result getGoodsList() {

        List<TbGoods> list = tbGoodsMapper.selectList(null);

        for (TbGoods tbGoods : list) {
            List<TbGoodsImage> tbGoodsImages = tbGoodsImageMapper.selectList(
                    new QueryWrapper<TbGoodsImage>().lambda().eq(TbGoodsImage::getGoodsId, tbGoods.getGoodsId())
            );
            tbGoods.setTbGoodsImages(tbGoodsImages);
        }


        return Result.ok().setData(list);
    }

    @Override
    public Result saveGoods(TbGoods tbGoods) {

        TbGoods goods = tbGoodsMapper.selectOne(
                new QueryWrapper<TbGoods>().lambda().eq(TbGoods::getGoodsName, tbGoods.getGoodsName())
        );
        if (goods!=null){
            return Result.error().setMsg("名称重复");
        }
        tbGoodsMapper.insert(tbGoods);

        for (TbGoodsImage tbGoodsImage : tbGoods.getTbGoodsImages()) {
            tbGoodsImage.setGoodsId(tbGoods.getGoodsId());
            tbGoodsImageMapper.insert(tbGoodsImage);
        }


        return Result.ok();
    }
}




